package com.interview.ujiuye1.test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

class Node{
    String code;
    String parentCode;
    public Node(String code,String parentCode){
        this.code = code;
        this.parentCode = parentCode;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getParentCode() {
        return parentCode;
    }

    public void setParentCode(String parentCode) {
        this.parentCode = parentCode;
    }
}

public class Demo1 {
    private static List<Node> nodeList = new ArrayList<>();
    private static List<String> lists = new ArrayList<>();//存储返回的字符串

    static {
        try
        {
            FileReader reader = new FileReader("myfile.txt");
            BufferedReader br = new BufferedReader(reader);
            while (br.ready()){
                String line = br.readLine();//读取一行
                String[] data = line.split(";");//分开
                String[] codes = data[0].split(":");
                String[] pcodes = data[1].split(":");
                Node node = new Node(codes[1],pcodes[1]);
                nodeList.add(node);
            }
            br.close();
            reader.close();
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        List<String> nodes = getLeafCode("node3");
        System.out.println(nodes);
    }
    public static List<String> getLeafCode(String code){
        for (Node node : nodeList){
            if (node.getParentCode().equals(code)){
                lists.add(node.getCode());
                getLeafCode(node.getCode());
            }
        }
        return lists;
    }
}
